package com.fita.entity.wallet;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

/**
 * 交易记录
 *
 * @description:
 * @author: 11200
 * @date: 2019/7/1
 */
@Entity
@Table(name = "transaction_record")
public class TransactionRecord {

    @Id
    @GeneratedValue
    private Long id;

    /**
     * 交易类型
     */
    @Enumerated(EnumType.STRING)
    private TransactionType transactionType;

    /**
     * 交易金额
     */
    private int amount;

    /**
     * 充值记录
     */
    @ManyToOne
    private RechargeRecord rechargeRecord;

    /**
     * 提现记录
     */
    @ManyToOne
    private WithdrawRecord withdrawRecord;

    /**
     * 代理分销奖励记录
     */
    @ManyToOne
    private DistributionRewardRecord distributionRewardRecord;

    @ManyToOne
    private RentCarRecord rentCarRecord;

    private Date createTime;

    /**
     * 目标钱包
     */
    @ManyToOne
    @JoinColumn(nullable = false)
    @JsonIdentityInfo(generator= ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private UserWallet targetUserWallet;

    @Column(name = "is_disabled")
    private Boolean disabled;

    /**
     *
     */
    public TransactionRecord() {
    }

    /**
     * 构造提现事务
     * @param withdrawRecord
     * @param userWallet
     */
    public TransactionRecord(WithdrawRecord withdrawRecord, UserWallet userWallet) {

        this.withdrawRecord = withdrawRecord;
        this.targetUserWallet = userWallet;
        this.disabled = false;
        this.transactionType = TransactionType.WITHDRAW;
        this.amount = withdrawRecord.getAmount();
        this.createTime = new Date();
    }

    // 构造充值事务
    public TransactionRecord(RechargeRecord recharge, UserWallet targetWallet) {

        this.rechargeRecord = recharge;
        this.targetUserWallet = targetWallet;
        this.createTime = new Date();
        this.disabled = false;
        this.transactionType = TransactionType.RECHARGE;
        this.amount = recharge.getAmount();
    }

    /**
     * 绑定分销奖励
     * @param distributionRewardRecord
     * @return
     */
    public TransactionRecord bindDistributionRewardRecord(DistributionRewardRecord distributionRewardRecord, UserWallet targetUserWallet){
        this.distributionRewardRecord = distributionRewardRecord;
        this.disabled = false;
        this.transactionType = TransactionType.AGENT_DISTRIBUTION_REWARD;
        this.targetUserWallet = targetUserWallet;
        this.amount = distributionRewardRecord.getAmount();
        this.createTime = new Date();
        return this;
    }

    /**
     * 获取人民币格式价格
     * @return
     */
    @Transient
    public BigDecimal getCnyAmount(){

        if (this.amount > 0 ){
            BigDecimal bigDecimal = new BigDecimal(this.amount).divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP);
            return bigDecimal;
        }
        return BigDecimal.ZERO;
    }

    /**
     * 获取交易后钱包余额
     * @return
     */
    @Transient
    public BigDecimal getCnyCurrentUserWalletAmount(){

        Integer amount = null;

        if (rechargeRecord != null){
            amount = rechargeRecord.getCurrentUserWalletAmount();
        }else if(withdrawRecord != null){
            amount = withdrawRecord.getCurrentUserWalletAmount();
        }else if (rentCarRecord != null){
            amount = rentCarRecord.getCurrentUserWalletAmount();
        }else if (distributionRewardRecord != null){
            amount = distributionRewardRecord.getCurrentUserWalletAmount();
        }

        if (amount == null){
            return null;
        }
        return new BigDecimal(amount).divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public TransactionType getTransactionType() {
        return transactionType;
    }

    public void setTransactionType(TransactionType transactionType) {
        this.transactionType = transactionType;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public RechargeRecord getRechargeRecord() {
        return rechargeRecord;
    }

    public void setRechargeRecord(RechargeRecord rechargeRecord) {
        this.rechargeRecord = rechargeRecord;
    }

    public WithdrawRecord getWithdrawRecord() {
        return withdrawRecord;
    }

    public void setWithdrawRecord(WithdrawRecord withdrawRecord) {
        this.withdrawRecord = withdrawRecord;
    }

    public DistributionRewardRecord getDistributionRewardRecord() {
        return distributionRewardRecord;
    }

    public void setDistributionRewardRecord(DistributionRewardRecord distributionRewardRecord) {
        this.distributionRewardRecord = distributionRewardRecord;
    }

    public UserWallet getTargetUserWallet() {
        return targetUserWallet;
    }

    public void setTargetUserWallet(UserWallet targetUserWallet) {
        this.targetUserWallet = targetUserWallet;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    public RentCarRecord getRentCarRecord() {

        return rentCarRecord;
    }

    public void setRentCarRecord(RentCarRecord rentCarRecord) {
        this.rentCarRecord = rentCarRecord;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}
